<template>
  <div class="recharge-card-management">
    <!-- 操作按钮 -->
    <el-button type="primary" icon="el-icon-plus" @click="openAddDialog">新增充值卡</el-button>

    <!-- 充值卡信息表格 -->
    <el-table :data="paginatedCards" style="width: 100%" stripe>
      <el-table-column label="编号" prop="id" width="150" />
      <el-table-column label="姓名" prop="cardas_name" width="200" />
      <el-table-column label="余额" prop="balance" width="150" />
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button type="text" size="small" @click="openEditDialog(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="deleteCard(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页控制和每页条数选择 -->
    <div class="pagination-wrapper">
      <el-pagination
          :current-page="currentPage"
          :page-size="pageSize"
          :total="cards.length"
          layout="total, prev, pager, next, jumper"
          @current-change="handlePageChange"
          class="pagination"
      />
      <el-select v-model="pageSize" class="page-size-select" @change="handlePageSizeChange" placeholder="每页显示条数">
        <el-option label="3条" :value="3"></el-option>
        <el-option label="5条" :value="5"></el-option>
        <el-option label="10条" :value="10"></el-option>
        <el-option label="20条" :value="20"></el-option>
      </el-select>
    </div>

    <!-- 新增/编辑充值卡弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px">
      <el-form :model="currentCard" :rules="rules" ref="cardForm" label-width="100px">
        <el-form-item label="编号" prop="id">
          <el-input v-model="currentCard.id" disabled placeholder="自动生成" />
        </el-form-item>
        <el-form-item label="姓名" prop="cardas_name">
          <el-input v-model="currentCard.cardas_name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="余额" prop="balance">
          <el-input v-model="currentCard.balance" placeholder="请输入余额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveCard">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cards: [
        { id: 1, cardas_name: '张三', balance: 100.50 },
        { id: 2, cardas_name: '李四', balance: 250.75 },
        { id: 3, cardas_name: '王五', balance: 300.00 }
      ],
      currentCard: { id: null, cardas_name: '', balance: '' },
      dialogVisible: false,
      dialogTitle: '新增充值卡',
      currentPage: 1,
      pageSize: 3,
      rules: {
        cardas_name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        balance: [{ required: true, message: '请输入余额', trigger: 'blur' }]
      }
    };
  },
  computed: {
    paginatedCards() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = this.currentPage * this.pageSize;
      return this.cards.slice(start, end);
    }
  },
  methods: {
    openAddDialog() {
      this.currentCard = { id: null, cardas_name: '', balance: '' };
      this.dialogTitle = '新增充值卡';
      this.dialogVisible = true;
    },
    openEditDialog(card) {
      this.currentCard = { ...card };
      this.dialogTitle = '编辑充值卡';
      this.dialogVisible = true;
    },
    saveCard() {
      this.$refs.cardForm.validate((valid) => {
        if (valid) {
          if (this.currentCard.id) {
            const index = this.cards.findIndex(item => item.id === this.currentCard.id);
            this.$set(this.cards, index, this.currentCard);
          } else {
            this.currentCard.id = this.cards.length + 1;
            this.cards.push(this.currentCard);
          }
          this.dialogVisible = false;
          this.$message.success(`${this.dialogTitle === '新增充值卡' ? '新增' : '编辑'}成功`);
        }
      });
    },
    deleteCard(card) {
      this.$confirm('确认删除该充值卡吗?', '删除充值卡', { type: 'warning' }).then(() => {
        this.cards = this.cards.filter(item => item.id !== card.id);
        this.$message.success('删除成功');
      }).catch(() => {});
    },
    handlePageChange(page) {
      this.currentPage = page;
    },
    handlePageSizeChange(size) {
      this.pageSize = size;
    }
  }
};
</script>

<style scoped>
.recharge-card-management {
  padding: 20px;
}

.dialog-footer {
  text-align: right;
}

.el-input[type="textarea"] {
  resize: none;
}

.pagination-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
}

.page-size-select {
  margin-left: 10px;
  width: 150px;
}

.pagination {
  margin-top: 20px;
  text-align: center;
}
</style>
